Skip to content

Check for ArrayStoreException on array update#971

Merged
dtim merged 1 commit intomainfrom
dtim/923_arraystoreexception
Sep 25, 2022
Merged

Check for ArrayStoreException on array update#971
dtim merged 1 commit intomainfrom
dtim/923_arraystoreexception

Conversation

@dtim
Copy link
Collaborator

@dtim dtim commented Sep 19, 2022

Description

This PR adds a check to TraversalContext.traverseAssignLeftPart to detect if ArrayStoreException should be thrown for the given array element assignment statement.

The exception is generated if the type of the value on the right-hand side of the assignment is not a subtype of the array elements type and is not null.

This change affects all code that explicitly assigns array elements, as well as all calls to Objects.arraycopy and similar Arrays static methods.

Fixes #923

Type of Change

Breaking change (fix or feature that would cause existing functionality to not work as expected)

New executions may be generated for the code that involves array modification.

How Has This Been Tested?

Automated Testing

All existing unit tests should pass.

A new test suite org.utbot.examples.arrays.ArrayStoreExceptionExamplesTest has been added, it should pass both with and without concrete execution.

Manual Scenario

Generate a test suite (with fuzzer turned off) for a method that assigns values to array elements of a incompatible reference type, e.g.:

public class Foo {
    public int foo() {
        Integer[] integers = { 1, 2, 3 };
        Object[] objects = integers;
        objects[1] = "";
        return 1;
    }
}

A single test should be generated, that corresponds to ArrayStoreException:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;

public class FooTest {
    ///region Test suites for executable basic.Foo.foo

    ///region SYMBOLIC EXECUTION ENGINE: ERROR SUITE for method foo()

    /**
     * @utbot.classUnderTest {@link Foo}
     * @utbot.methodUnderTest {@link Foo#foo()}
     * @utbot.throwsException {@link ArrayStoreException} in: objects[1] = "";
     */
    @Test
    @DisplayName("foo: objects[1] = \"\" -> ThrowArrayStoreException")
    public void testFoo_IntegerValueOf() {
        Foo foo = new Foo();
        
        /* This test fails because method [basic.Foo.foo] produces [java.lang.ArrayStoreException: java.lang.String]
            basic.Foo.foo(Foo.java:9) */
        foo.foo();
    }
    ///endregion

    ///endregion
}

Checklist

This is the author self-check list

  • The change followed the style guidelines of the UTBot project
  • Self-review of the code is passed
  • The change contains enough commentaries, particularly in hard-to-understand areas
  • New documentation is provided or existed one is altered
  • No new warnings
  • New tests have been added
  • All tests pass locally with my changes

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect ArrayStoreException on illegal array writes

2 participants